package com.aaa.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;


@Configuration
@EnableWebSecurity //启动安全框架
@EnableGlobalMethodSecurity(prePostEnabled = true)//开启注解方法的角色权限控制
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

//    @Autowired
//    private CustomerUserDetailService customerUserDetailService;

    //自定义认证
    @Resource
    private LoginValidateAuthenticationProvider loginValidateAuthenticationProvider;

    //登录成功handler
    @Resource
    private LoginSuccessHandler loginSuccessHandler;

    //登录失败handler
    @Resource
    private LoginFailureHandler loginFailureHandler;

    //设置用户名,密码,角色
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这里要设置自定义认证
        auth.authenticationProvider(loginValidateAuthenticationProvider);

    }

//    用户的登录密码进行加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

//    静态资源放行
    @Override
    public void configure(WebSecurity web) throws Exception {
       // web.ignoring().antMatchers("/bootstrap/**","/Semantic-UI/**","/layui/**","/css/**", "/js/**", "/plugins/**", "/img/**", "/fonts/**");
        web.ignoring().antMatchers("/element-ui/**","/bootstrap/**","/Semantic-UI/**","/layui/**","/css/**", "/js/**", "/plugins/**", "/img/**", "/fonts/**");
    }

    //设置拦截除了登录界面其他任何请求都需要验证
    protected void configure(HttpSecurity http) throws Exception {
        http.headers().frameOptions().disable();
        http.authorizeRequests()
                .antMatchers("/test","/login","/register","/verifyCode","/verify","/user","/reg_info")
                .permitAll() //放行跳转到登录界面
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login")//更改登陆页面
                .permitAll()
//                .failureUrl("/login")
                .defaultSuccessUrl("/home",true)//登录成功后的默认页面
                .successHandler(loginSuccessHandler)//成功登录处理器
                .failureHandler(loginFailureHandler)//失败登录处理器
                .and()
                .csrf().disable();//必须加
//        注销地址
        http.logout()
                //注销之后 跳转的页面
                .logoutSuccessUrl("/login")
                //配置自己的注销URL
                .logoutUrl("/logout")
                //是否销毁session
                .invalidateHttpSession(true)
                //删除指定的cookies
                .deleteCookies();
    }
}
